package com.example.springboot.modules.order.service;

import com.example.springboot.common.utils.JsonResult;
import com.example.springboot.modules.order.mapper.PurchaseMapper;
import com.example.springboot.modules.order.mapper.SupplierMapper;
import com.example.springboot.modules.order.model.Purchase;
import com.example.springboot.modules.order.vo.PurchaseVo;
import com.github.pagehelper.PageHelper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tk.mybatis.mapper.entity.Example;

import java.util.ArrayList;
import java.util.List;

/**
 * Created by zql on 2018/7/15.
 */
@Service
public class PurchaseService {
    @Autowired
    private PurchaseMapper purchaseMapper;
    @Autowired
    private SupplierMapper supplierMapper;

    public List<Purchase> selectAll(Purchase purchase){

        if (purchase.getPage() != null && purchase.getRows() != null) {
            PageHelper.startPage(purchase.getPage(), purchase.getRows());
        }
        Example example = new Example(Purchase.class);
        example.orderBy("createtime").desc();
        Example.Criteria criteria = example.createCriteria();
       if (purchase.getId() != null) {
            criteria.andLike("id", "%" + purchase.getId() + "%");
        }
       /*  if (country.getCountrycode() != null && country.getCountrycode().length() > 0) {
            criteria.andLike("countrycode", "%" + country.getCountrycode() + "%");
        }*/
        return purchaseMapper.selectByExample(example);

    }

    public List<PurchaseVo> parseVoList(List<Purchase> list) {
        List<PurchaseVo> purchaseVos=new ArrayList<>();
        for (Purchase purchase:list){
            purchaseVos.add(parseVo(purchase));
        }
        return purchaseVos;
    }

    /**
     *
     * @param purchase
     * @return
     */
    public PurchaseVo parseVo(Purchase purchase) {
            PurchaseVo vo=new PurchaseVo(purchase);
            vo.setSupplier(supplierMapper.selectByPrimaryKey(vo.getSupplierid()));
        return vo;
    }


}
